#include "skinmanager.h"
#include <QDebug>
#include <QDir>

static int g_lastSkin = 0;
static bool init = true;
static QStringList g_skinsPath = {
    QDir::currentPath()+"/../skin1/skin1_main.rcc",
    QDir::currentPath()+"/../skin2/skin2_main.rcc"
};
static QStringList g_imagePath = {
    QDir::currentPath()+"/../skin1/skin1_image.rcc",
    QDir::currentPath()+"/../skin2/skin2_image.rcc"
};

static QStringList g_qmlPath = {
    "skin1/skin1_main.qml",
    "skin2/skin2_main.qml"
};

SkinManager::SkinManager(QQuickView* view, QObject *parent)
    : QObject(parent)
{
    m_currentSkin = "";
    _view = view;
    _view->rootContext()->setContextProperty("SkinManager", this);
}

void SkinManager::changeskin(int skin)
{
    qInfo()<<"SkinManager changeskin "<<skin;
    qInfo()<<QDir::currentPath();

    if(init){
        init = false;
        bool r = QResource::registerResource(g_skinsPath.at(skin));
        r = QResource::registerResource(g_imagePath.at(skin));
        if(r){
            setCurrentSkin(g_qmlPath.at(skin));
        }
    }

    if(g_lastSkin == skin) return;

    QResource::unregisterResource(g_skinsPath.at(g_lastSkin));
    QResource::unregisterResource(g_imagePath.at(g_lastSkin));
    bool r = QResource::registerResource(g_skinsPath.at(skin));
    r = QResource::registerResource(g_imagePath.at(skin));
    if(r){
        setCurrentSkin(g_qmlPath.at(skin));
    }

    g_lastSkin = skin;
}

